import Vue from "vue";
import Router from "vue-router";
// import HelloWorld from '@/components/HelloWorld'

Vue.use(Router);
// import Login from "../views/login/login.vue";
// import Home from "../views/home/home.vue";
// import Welcome from "../views/welcome/welcome.vue";
// import Users from "../views/user/user.vue";
// import Rights from "../views/right/rights.vue";
// import Roles from "../views/right/roles.vue";
// import Category from "../views/category/category.vue";
// import Goods from "../views/goods/goods.vue";
// import Params from "../views/params/params.vue";
// import Orders from "../views/orders/orders.vue";
const router = new Router({
  routes: [
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login/login.vue")
    },
    {
      path: "/",
      name: "home",
      component: () => import("@/views/home/home.vue"),
      redirect: {
        path: "welcome"
      },
      children: [
        {
          name: "welcome",
          path: "welcome",
          component: () => import("@/views/welcome/welcome.vue")
        },
        {
          path: "users",
          name: "users",
          component: () => import("@/views/user/user.vue")
        },
        {
          name: "rights",
          path: "rights",
          component: () => import("@/views/right/rights.vue")
        },
        {
          name: "roles",
          path: "roles",
          component: () => import("@/views/right/roles.vue")
        },
        {
          name: "category",
          path: "categories",
          component: () => import("@/views/category/category.vue")
        },
        {
          name: "goods",
          path: "goods",
          component: () => import("@/views/goods/goods")
        },
        {
          name: "AddGoods",
          path: "toadd",
          component: () => import("@/views/addGoods/addGoods")
        },
        {
          name: "params",
          path: "params",
          component: () => import("@/views/params/params.vue")
        },
        {
          name: "orders",
          path: "orders",
          component: () => import("@/views/orders/orders.vue")
        },
        {
          path: "reports",
          name: "reports",
          component: () => import("@/views/reports/reports.vue")
        }
      ]
    }
  ]
});
router.beforeEach((to, from, next) => {
  // 1.从本地缓存中获取token
  let token = window.localStorage.getItem("tokenLogin");
  // token存在  放行
  if (token) {
    next();
  } else {
    // token不存在 判断用户路由  是登录页面则放行  不是则跳转到登录页面
    if (to.path == "/login") {
      next();
    } else {
      next("/login");
    }
  }
});
export default router;
